<?php

/**
 * This is the model class for table "ykl_user".
 *
 * The followings are the available columns in table 'ykl_user':
 * @property integer $id
 * @property string $password
 * @property string $mobile
 * @property string $token
 * @property string $ip
 * @property string $mac
 * @property integer $uptime
 * @property integer $comeing
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ykl_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('mobile', 'required' ,'message'=>'手机不能为空'),
			array('password', 'required' ,'message'=>'密码不能为空'),
			array('uptime, comeing', 'numerical', 'integerOnly'=>true),
			array('password, token, ip, mac', 'length', 'max'=>128),
			array('password', 'length', 'min'=>4,'tooShort'=>'密码位数错误'),
			//array('mobile', 'length', 'max'=>16),
            //array('mobile', 'match', 'allowEmpty'=>true, 'pattern'=>'/^13[0-9]{1}[0-9]{8}$|15[012356789]{1}[0-9]{8}$|18[012356789]{1}[0-9]{8}$|14[57]{1}[0-9]$/','message'=>'手机号码格式不正确'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, password, mobile, token, ip, mac, uptime,lasttime, comeing', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'mobile' => '手机',
			'password' => '密码',
			'token' => 'Token',
			'ip' => 'Ip',
			'mac' => 'Mac',
			'uptime' => '连接时长',
			'comeing' => '登陆次',
            'lasttime'=>'最后登陆时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('token',$this->token,true);
		$criteria->compare('ip',$this->ip,true);
		$criteria->compare('mac',$this->mac,true);
		$criteria->compare('uptime',$this->uptime);
		$criteria->compare('lasttime',$this->lasttime);
		$criteria->compare('comeing',$this->comeing);
        $criteria->order="id desc";

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * 给用户发送短消息
	 * @return 
	 */

     public function sendSMS($mobile, $content) {

     	 $http = 'http://api.sms7.cn/mt/'; //短信接口
         $uid = 'xayoudao'; //用户账号
         $pwd = '1qazxsw2@yd'; //密码
         $time = '';
         $mid = '';

        $data = array
        (
            'uid' => $uid, //用户账号
            'pwd' => md5($pwd . $uid), //MD5位32密码,密码和用户名拼接字符
            'mobile' => $mobile, //号码
            'content' => $content, //内容
            'mobileids' => md5($mobile.time()),
            'time' => $time, //定时发送
            'encode'=>'utf8',
        );
        $re = $this->postSMS($http, $data); //POST方式提交
        parse_str($re);
        if ($stat == '100') {
            return "success";
        } else {
            $messages = iconv('GBK','UTF-8',$re);
            return $messages;
        }
    }

    /**
     * 正式发送消息
     *
     * @return void
     * @author 
     **/ 
    private function postSMS($url, $data = '') {
        $row = parse_url($url);
        $host = $row['host'];
        $port = 80;
        $file = $row['path'];
        $post='';
        while (list($k, $v) = each($data)) {
            $post .= rawurlencode($k) . "=" . rawurlencode($v) . "&"; //转URL标准码
        }
        $post = substr($post, 0, -1);
        $len = strlen($post);
        $fp = @fsockopen($host, $port, $errno, $errstr, 10);
        if (!$fp) {
            return "$errstr ($errno)\n";
        } else {
            $receive = '';
            $out = "POST $file HTTP/1.1\r\n";
            $out .= "Host: $host\r\n";
            $out .= "Content-type: application/x-www-form-urlencoded\r\n";
            $out .= "Connection: Close\r\n";
            $out .= "Content-Length: $len\r\n\r\n";
            $out .= $post;
            fwrite($fp, $out);
            while (!feof($fp)) {
                $receive .= fgets($fp, 128);
            }
            fclose($fp);
            $receive = explode("\r\n\r\n", $receive);
            unset($receive[0]);
            return implode("", $receive);
        }
    }
}